#include "StdAfx.h"
#include "stdio.h"
#include "xhRGXray.h"

#include "DDXray.h"

int CreatePlugMan(RayPlug **ppPlug)
{
	xhRGXray* pPlug = new xhRGXray;

	if(NULL != pPlug)
	{
		*ppPlug = (RayPlug*)pPlug;

		return DD_ERR_SUCCESS;
	}

	return DD_ERR_FAILURE;
}

xhRGXray::xhRGXray(void)
{
	m_bValid = FALSE;
}

xhRGXray::~xhRGXray(void)
{
}

int xhRGXray::Initialize()
{
	PlugReg::SetPlugFlag("PlugRay\\rgxray.a");

	return 0;
}


//
int xhRGXray::OpParam(void *pVoid)
{
	if( NULL == pVoid )
	{
		return DD_ERR_PARAMETER;
	}
	int nRet = DD_ERR_SUCCESS;

	zkPlugParam *pPlugParam = (zkPlugParam*)pVoid;

	switch( pPlugParam->dwType )
	{
	case ePlug_Parma_GetRegisterInfo:
		{
			pPlugParam->bAutoDel = FALSE;
			pPlugParam->pParam   = (void*)&m_xRegParam;
		}
		break;
	case ePlug_Parma_SetRegisterInfo:
		{
			zkPlugRegParam *pReg = (zkPlugRegParam *)pPlugParam->pParam;
			if( NULL != pReg && sizeof(zkPlugRegParam) == pPlugParam->nParamSize )
			{
				PlugReg::SetRegisterCode( pReg->szRegisterCode );
			}
		}
		break;
	default:
		break;
	}

	return 0;
}
//

int xhRGXray::SetComCfg(zkComCfg *pCfg)
{
	m_xComCfg = *pCfg;

	BOOL bRet = m_xCom.Open(    
		m_xComCfg.nCOM,
		m_xComCfg.nBaudRate,
		m_xComCfg.nCheck,
		m_xComCfg.nStopBit,
		m_xComCfg.nDataBit
		); 

	if( bRet )
	{
		return 0;
	}

	return -1;
}

int xhRGXray::CheckConnect(int &nStatus)
{
	if( NULL != m_xCom.m_hCom )
	{
		nStatus = 1;
	}
	else
	{
		nStatus = 0;
	}
	return 0;
}

int xhRGXray::SetRayCfg(zkRayCfg *pCfg)
{
	m_bValid = FALSE;

	if( NULL == pCfg )
	{
		return -1;
	}

	if(   0 < pCfg->fFOCKV[0] && pCfg->fFOCKV[0] < pCfg->fFOCKV[1] && pCfg->fFOCKV[1] < pCfg->fFOCKV[2] && pCfg->fFOCKV[2] < pCfg->fFOCKV[3]  
	   && 0 < pCfg->fFOCMA[3] && pCfg->fFOCMA[3] <= pCfg->fFOCMA[2] && pCfg->fFOCMA[2] <= pCfg->fFOCMA[1] && pCfg->fFOCMA[1] <= pCfg->fFOCMA[0] 
	   && 0 < pCfg->fsfockv[0] && pCfg->fsfockv[0] < pCfg->fsfockv[1] && pCfg->fsfockv[1] < pCfg->fsfockv[2] && pCfg->fsfockv[2] < pCfg->fsfockv[3]  
	   && 0 < pCfg->fsfocma[3] && pCfg->fsfocma[3] <= pCfg->fsfocma[2] && pCfg->fsfocma[2] <= pCfg->fsfocma[1] && pCfg->fsfocma[1] <= pCfg->fsfocma[0] 
	)
	{
		m_xRayCfg = *pCfg;
		m_bValid  = TRUE;
		return 0;
	}

	return -1;
}

int xhRGXray::SetRayCtrl(zkRayCtrl *pRayCtrl,BOOL bWaitToGo)
{
	if( m_xRegParam.dwReg == 0 )
	{
		return DD_ERR_NOTREGXRAYDEV;
	}
    if( !m_bValid )
	{
		return -1;
	}

	m_xSetRayCtrl    = *pRayCtrl;
	zkRayCtrl *pCtrl = &m_xSetRayCtrl;

	if( pCtrl->nReset == 1 )
	{
		if( m_xRayCfg.xRayCfg[eSetDog].ucAsk )
		{
			WriteCommand(m_xRayCfg.xRayCfg[eSetDog].cAskCmd);
		}
		if( m_xRayCfg.xRayCfg[eReset].ucAsk )
		{
			WriteCommand(m_xRayCfg.xRayCfg[eReset].cAskCmd);
		}
	}
	else if( pCtrl->nOpenHighV == 1 )
	{
        ChangeMaxMA(pCtrl,&m_xRayCfg);

		StartRadialData(pCtrl);
	}
	else if( pCtrl->nOpenHighV == 0 )
	{
		StopRadialData();
	}

	return 0;
}




int xhRGXray::StartRadialData(zkRayCtrl *pRayCtrl)
{
	int  nRet = 0;
	BYTE byCommand[255];
	int  nOffset = 0;
 
	if( m_xRayCfg.xRayCfg[eSetDog].ucAsk )
	{
		WriteCommand(m_xRayCfg.xRayCfg[eSetDog].cAskCmd);
	}

	m_xOutRayCtrl.nOpenHighV = 1;

//  <STX>VPXXXX<CR>
	if( m_xRayCfg.xRayCfg[eSetKV].ucAsk )
	{
		nOffset = 0;
		if( m_xRayCfg.xRayCfg[eBeginChar].ucAsk )AddStrToString( m_xRayCfg.xRayCfg[eBeginChar].cAskCmd,byCommand,nOffset); //<STX>
		AddStrToString( m_xRayCfg.xRayCfg[eSetKV].cAskCmd,byCommand,nOffset);     //VP
		CalVoltageString( pRayCtrl->fKV,byCommand,nOffset);       //VVVV
		if( m_xRayCfg.xRayCfg[eEndChar].ucAsk )AddStrToString( m_xRayCfg.xRayCfg[eEndChar].cAskCmd,byCommand,nOffset);     //<CR>

		if( m_xRayCfg.xRayCfg[eEnterchar].ucAsk )
		{
			AddEnterToString(byCommand,nOffset);
			//AddStrToString( m_xRayCfg.szEnter,byCommand,nOffset);       //Enter
		}
		nRet |= m_xCom.WriteData( (void*)byCommand,nOffset );

		//read ka
		m_xCom.ReadData( (void*)byCommand,200,m_xComCfg.nTimeout );

		m_xOutRayCtrl.fKV = pRayCtrl->fKV;
	}

//  <STX>CPXXXXX<CR>
	if( m_xRayCfg.xRayCfg[eSetMA].ucAsk )
	{
		nOffset = 0;
		if( m_xRayCfg.xRayCfg[eBeginChar].ucAsk )AddStrToString( m_xRayCfg.xRayCfg[eBeginChar].cAskCmd,byCommand,nOffset); //<STX>
		AddStrToString( m_xRayCfg.xRayCfg[eSetMA].cAskCmd,byCommand,nOffset);     //CP
		CalAmpereString( pRayCtrl->fMA,byCommand,nOffset);        //CCCC
		if( m_xRayCfg.xRayCfg[eEndChar].ucAsk )AddStrToString( m_xRayCfg.xRayCfg[eEndChar].cAskCmd,byCommand,nOffset);     //<CR>

		if( m_xRayCfg.xRayCfg[eEnterchar].ucAsk )
		{
			AddEnterToString(byCommand,nOffset);
			//AddStrToString( m_xRayCfg.szEnter,byCommand,nOffset);       //Enter
		}
		nRet |= m_xCom.WriteData( (void*)byCommand,nOffset );

		//read ma
		m_xCom.ReadData( (void*)byCommand,200,m_xComCfg.nTimeout );

		m_xOutRayCtrl.fMA = pRayCtrl->fMA;

	}
	if( m_xRayCfg.xRayCfg[eBigFocus].ucAsk && pRayCtrl->nFocus == 1 )
	{
		WriteCommand(m_xRayCfg.xRayCfg[eBigFocus].cAskCmd);
	}
	if( m_xRayCfg.xRayCfg[eSmallFocus].ucAsk && pRayCtrl->nFocus == 0 )
	{
		WriteCommand(m_xRayCfg.xRayCfg[eSmallFocus].cAskCmd);
	}

	if( m_xRayCfg.xRayCfg[eExposalTime].ucAsk )
	{
		WriteCommand(m_xRayCfg.xRayCfg[eExposalTime].cAskCmd);
	}
	if( m_xRayCfg.xRayCfg[eReset].ucAsk )
	{
		WriteCommand(m_xRayCfg.xRayCfg[eReset].cAskCmd);
	}
	if( m_xRayCfg.xRayCfg[eOther1].ucAsk )
	{
		WriteCommand(m_xRayCfg.xRayCfg[eOther1].cAskCmd);
	}
	if( m_xRayCfg.xRayCfg[eOther2].ucAsk )
	{
		WriteCommand(m_xRayCfg.xRayCfg[eOther2].cAskCmd);
	}

	return 0;
}


void xhRGXray::StopRadialData()
{
	if( m_xRayCfg.xRayCfg[eSetDog].ucAsk  )
	{
		WriteCommand(m_xRayCfg.xRayCfg[eSetDog].cAskCmd);
	}
	if( m_xRayCfg.xRayCfg[eHighVoltageClose].ucAsk )
	{
		WriteCommand(m_xRayCfg.xRayCfg[eHighVoltageClose].cAskCmd);
	}

	m_xOutRayCtrl.fKV = 0;
	m_xOutRayCtrl.fMA = 0;
	m_xOutRayCtrl.nOpenHighV = 0;
}


int xhRGXray::GetRayCtrl(zkRayCtrl **ppRayCtrl)
{
	int  nRet    = 0;
	BYTE byCommand[255]={0};
	int  nOffset = 0;

	*ppRayCtrl = &m_xOutRayCtrl;

	return 0;
}


int xhRGXray::WriteCommand(char *pszCmd)
{
	int  nRet    = 0;
	BYTE byCommand[255];
	int  nOffset = 0;

	//off
	nOffset = 0;
	if( m_xRayCfg.xRayCfg[eBeginChar].ucAsk )AddStrToString( m_xRayCfg.xRayCfg[eBeginChar].cAskCmd,byCommand,nOffset);       //<STX>
	AddStrToString( pszCmd,byCommand,nOffset);
	if( m_xRayCfg.xRayCfg[eEndChar].ucAsk )AddStrToString( m_xRayCfg.xRayCfg[eEndChar].cAskCmd,byCommand,nOffset);     //<CR>

	if( m_xRayCfg.xRayCfg[eEnterchar].ucAsk  )
	{
		AddEnterToString(byCommand,nOffset);
		//AddStrToString( m_xRayCfg.szEnter,byCommand,nOffset);       //Enter
	}

	nRet = m_xCom.WriteData( (void*)byCommand,nOffset );

	nRet = m_xCom.ReadData( (void*)byCommand,200,m_xComCfg.nTimeout );

	return nRet;
}

void xhRGXray::AddEnterToString(BYTE *pBuffer,int &nOffset)
{
	pBuffer[nOffset++] = 0x0D;
}

void xhRGXray::AddStrToString(char *pCfg,BYTE *pBuffer,int &nOffset)
{
	int nLength = strlen( pCfg );
	if( nLength > 50 )
	{
		nLength = 50;
	}
	for( int i=0;i<nLength;i++ )
	{
		pBuffer[nOffset++] = (unsigned char)pCfg[i];
	}
}

void xhRGXray::CalVoltageString(float fVoltage,BYTE *pBuffer,int &nLen)
{
	if( fVoltage > 999.9 )
	{
		return;
	}

	int nValue = 0;
	int nRemain = (int)( fVoltage*10 );

	nValue = nRemain/1000;	
	*(pBuffer+nLen) = '0' + nValue;
	nLen++;
	nRemain = nRemain%1000;

	nValue = nRemain/100;	
	*(pBuffer+nLen) = '0' + nValue;
	nLen++;
	nRemain = nRemain%100;

	nValue = nRemain/10;	
	*(pBuffer+nLen) = '0' + nValue;
	nLen++;
	nRemain = nRemain%10;

	nValue = nRemain;	
	*(pBuffer+nLen) = '0' + nValue;
	nLen++;
}

void xhRGXray::CalAmpereString(float fAmpere,BYTE *pBuffer,int &nLen)
{
	if( fAmpere > 99.99 )
	{
		return;
	}
	int nValue = 0;
	int nRemain = (int)( fAmpere*100);


	nValue = nRemain/1000;	
	*(pBuffer+nLen) = '0' + nValue;
	nLen++;
	nRemain = nRemain%1000;

	nValue = nRemain/100;	
	*(pBuffer+nLen) = '0' + nValue;
	nLen++;
	nRemain = nRemain%100;

	nValue = nRemain/10;	
	*(pBuffer+nLen) = '0' + nValue;
	nLen++;
	nRemain = nRemain%10;

	nValue = nRemain;	
	*(pBuffer+nLen) = '0' + nValue;
	nLen++;
}


